<?php


namespace app\admin\controller\kb;

use app\common\controller\Adminbase;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\Db;

class Boardbk extends Adminbase
{
//    protected $searchFields = 'id,username,nickname';

    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        $sq_id = $this->request->param('sq_id/d', 0);
        $shop_id = $this->request->param('shop_id/d', 0);
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            $where[] = ['is_del', '=', 0];
            $where[] = ['bk_sq_id', '=', $sq_id];
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['id', '=', $filter['id']];
                }
                if (isset($filter['sim'])) {
                    $where[] = ['sim', 'like', '%' . $filter['sim'] . '%'];
                }

                if (isset($filter['is_bk'])) {
                    $where[] = ['is_bk', '=', $filter['is_bk']];
                }

                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['create_time', 'between', [$create_time[0], $create_time[1]]];
                }

                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['bk_time'])) {
                    $create_time = explode(' - ', $filter['bk_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['bk_time', 'between', [$create_time[0], $create_time[1]]];
                }

                if (isset($filter['hb_time'])) {
                    $create_time = explode(' - ', $filter['hb_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['hb_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            $_list = Db::name('board')
                ->page($page, $limit)
                ->where($where)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($v['bk_time']) {
                    $_list[$k]['bk_time'] = date('Y-m-d H:i:s', $v['bk_time']);
                } else {
                    $_list[$k]['bk_time'] = '------';
                }
                if ($v['hb_time']) {
                    $_list[$k]['hb_time'] = date('Y-m-d H:i:s', $v['hb_time']);
                } else {
                    $_list[$k]['hb_time'] = '------';
                }
                if ($v['shop_id']) {
                    $shop_info = Db::name('shop')->find($v['shop_id']);
                    $_list[$k]['shop_name'] = $shop_info['shop_name'];
                }
            }
            $total = Db::name('board')->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign([
            'sq_id' => $sq_id,
            'shop_id' => $shop_id
        ]);
        return $this->fetch();
    }


    public function index2()
    {
        $sq_id = $this->request->param('sq_id/d', 0);
        $shop_id = $this->request->param('shop_id/d', 0);
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            $where[] = ['is_del', '=', 0];
            $where[] = ['bk_sq_id', '=', $sq_id];
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['id', '=', $filter['id']];
                }
                if (isset($filter['sim'])) {
                    $where[] = ['sim', 'like', '%' . $filter['sim'] . '%'];
                }

                if (isset($filter['is_bk'])) {
                    $where[] = ['is_bk', '=', $filter['is_bk']];
                }

                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['create_time', 'between', [$create_time[0], $create_time[1]]];
                }

                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['bk_time'])) {
                    $create_time = explode(' - ', $filter['bk_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['bk_time', 'between', [$create_time[0], $create_time[1]]];
                }

                if (isset($filter['hb_time'])) {
                    $create_time = explode(' - ', $filter['hb_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['hb_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            $_list = Db::name('board')
                ->page($page, $limit)
                ->where($where)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($v['bk_time']) {
                    $_list[$k]['bk_time'] = date('Y-m-d H:i:s', $v['bk_time']);
                } else {
                    $_list[$k]['bk_time'] = '------';
                }
                if ($v['hb_time']) {
                    $_list[$k]['hb_time'] = date('Y-m-d H:i:s', $v['hb_time']);
                } else {
                    $_list[$k]['hb_time'] = '------';
                }
                if ($v['shop_id']) {
                    $shop_info = Db::name('shop')->find($v['shop_id']);
                    $_list[$k]['shop_name'] = $shop_info['shop_name'];
                }
            }
            $total = Db::name('board')->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign([
            'sq_id' => $sq_id,
            'shop_id' => $shop_id
        ]);
        return $this->fetch();
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');


        if (Db::name('board')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //删除
    public function del()
    {

        $ids = $this->request->param('ids/a', null);

        try {
            foreach ($ids as $id) {
                Db::name('board')->where('id', $id)->delete();
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    //取消
    public function cancal()
    {
        $id = $this->request->param('id/d', null);
        try {
            $board_info=Db::name('board')->find($id);
            $bk_sq_id=$board_info['bk_sq_id'];

            Db::name('board')->where('id', $id)->update([
                'shop_id' => 0,
                'bk_time' => '',
                'is_bk' => 0,
                'bk_sq_id' => 0
            ]);

            $send_num = Db::name('board')->where([
                'bk_sq_id' => $bk_sq_id,
                'is_del' => 0
            ])->count();

            Db::name('bk_sq')->update([
                'id' => $bk_sq_id,
//                'status' => 4,
                'send_num' => $send_num
            ]);

        } catch (\Exception $e) {
            return ['code' => 100, 'data' => '', 'msg' => $e->getMessage()];
        }
        return ['code' => 200, 'data' => '', 'msg' => '已取消'];
    }


    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $insert_data = array(
                'sim' => $data['sim'],
                'create_time' => time(),
            );
            $stmt = Db::name('board')->insertGetId($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {
            return $this->fetch();
        }
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $update_data = array(
                'id' => $data['id'],
                'sim' => $data['sim'],
            );
            $stmt = Db::name('board')->update($update_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }
        $id = $this->request->param('id/d', 0);

        $data = Db::name('board')->find($id);
        $this->assign('data', $data);
        return $this->fetch();
    }

    //导入 import
    public function import()
    {
        $sq_id = $this->request->param('sq_id/d', 0);
        $shop_id = $this->request->param('shop_id/d', 0);

        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['suffix'] = explode('.', $data['excel_url']);
            $data['suffix'] = end($data['suffix']);
            $allow_type = array(
                'xlsx', 'xls', 'csv', 'xlsm', 'xlsb', 'xltm'
            );
            $pattern = '/(http|https):\/\/[^\/]+/';

            $newUrl = preg_replace($pattern, '', $data['excel_url']);

            $newUrl = substr($newUrl, 2);

            if (in_array($data['suffix'], $allow_type)) {
                //uploads/files/20251017/50ca85b3d8aef700eba12dec4a5808dc.xlsx 路径默认
                $spreadsheet = IOFactory::load($newUrl);

                $sheetData = $spreadsheet->getActiveSheet()->toArray();

                unset($sheetData[0]);
                $import_data = array();
                foreach ($sheetData as $k => $v) {
                    if ($v[0]) {

                        $board_info = Db::name('board')->where([
                            'sim' => $v[0]
                        ])->find();
                        if ($board_info) {
                            if ($board_info['is_bk'] == 0) {
                                $update_data = array(
                                    'id' => $board_info['id'],
                                    'shop_id' => $shop_id,
                                    'bk_time' => time(),
                                    'is_bk' => 1,
                                    'bk_sq_id' => $sq_id
                                );
                                Db::name('board')->update($update_data);
                            }
                        } else {
                            $import_data[$k]['sim'] = $v[0];
                            $import_data[$k]['create_time'] = time();
                            $import_data[$k]['shop_id'] = $shop_id;

                            $import_data[$k]['bk_time'] = time();
                            $import_data[$k]['is_bk'] = 1;
                            $import_data[$k]['bk_sq_id'] = $sq_id;
                        }

                    }
                }
                if ($import_data) {
                    $result = Db::name('board')->insertAll($import_data);
                }
                $send_num = Db::name('board')->where([
                    'bk_sq_id' => $sq_id,
                    'is_del' => 0
                ])->count();
                Db::name('bk_sq')->update([
                    'id' => $sq_id,
                    'status' => 4,
                    'send_num' => $send_num
                ]);

                $this->success("导入完成");
            } else {
                $this->error('请上传正确格式文件');
            }
        }
        $data = $this->request->param();
        $this->assign([
            'sq_id' => $sq_id,
            'shop_id' => $shop_id
        ]);
        return $this->fetch();
    }

    public function import1()
    {
        $sq_id = $this->request->param('sq_id/d', 0);
        $shop_id = $this->request->param('shop_id/d', 0);

        if ($this->request->isPost()) {
            $data = $this->request->post();
//            $data_list=Db::name('board')
//                ->where([
//                    ['sim','between',[$data['sim1'],$data['sim2']]],
//                    ['is_bk','=',0]
//                ])->select();
            Db::name('board')
                ->where([
                    ['sim', 'between', [$data['sim1'], $data['sim2']]],
                    ['is_bk', '=', 0]
                ])
                ->update([
                    'shop_id' => $shop_id,
                    'bk_time' => time(),
                    'is_bk' => 1,
                    'bk_sq_id' => $sq_id
                ]);
            $send_num = Db::name('board')->where([
                'bk_sq_id' => $sq_id,
                'is_del' => 0
            ])->count();
            Db::name('bk_sq')->update([
                'id' => $sq_id,
                'status' => 4,
                'send_num' => $send_num
            ]);
            $this->success("划拨完成");
        }
        $data = $this->request->param();
        $this->assign([
            'sq_id' => $sq_id,
            'shop_id' => $shop_id
        ]);
        return $this->fetch();
    }

    public function import2()
    {
        $sq_id = $this->request->param('sq_id/d', 0);
        $shop_id = $this->request->param('shop_id/d', 0);

        if ($this->request->isPost()) {
            $data = $this->request->post();
            $sims = explode( ';',$data['sim']);
            //todo 筛选已激活
            Db::name('board')
                ->where([
                    ['sim', 'in', $sims],
                    ['is_bk', '=', 0]
                ])
                ->update([
                    'shop_id' => $shop_id,
                    'bk_time' => time(),
                    'is_bk' => 1,
                    'bk_sq_id' => $sq_id
                ]);
            $send_num = Db::name('board')->where([
                'bk_sq_id' => $sq_id,
                'is_del' => 0
            ])->count();
            Db::name('bk_sq')->update([
                'id' => $sq_id,
                'status' => 4,
                'send_num' => $send_num
            ]);
            $this->success("划拨完成");
        }
        $data = $this->request->param();
        $this->assign([
            'sq_id' => $sq_id,
            'shop_id' => $shop_id
        ]);
        return $this->fetch();
    }
}
